﻿/*CLR Version: 4.0.30319.18408
 * Creat Date: 2017-11-21 17:18:13
 * Creat Year: 2017
 * Creator: 程炜.Snail
 */
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utility.Configuration
{
    /*
    1. configSections 增加配置
    <section name="DBConnectionString" type="Utility.Configuration.DBConnectionStringSection,Utility" />

    2. configuration 增加节点配置, Master/Backup节点中子项 default 值作为查找不到对应数据库名称默认返回的链接字符串使用
       ** Master节点是必要节点,Backup节点允许为空 **
       ** type =  Oracle | SqlServer ***************
      <DBConnectionString>
        <Master>
          <add key="DB1Connection" value="DB1" type="Oracle" default="true"/>
          <add key="DB2Connection" value="DB2" type="SqlServer" default="false"/>
        </Master>
        <Backup>
          <add key="DB1Connection" value="DB1_2" type="Oracle" default="true"/>
          <add key="DB2Connection" value="DB2_2" type="SqlServer" default="false"/>
        </Backup>
      </DBConnectionString>
    
    3. 使用
    var dbStrings  = ConfigurationManager.GetSection("DBConnectionString") as  DBConnectionStringSection;
    string db1= dbStrings.Master["DB1Connection"].Value;
    string db1Backup= dbStrings.Backup["DB1Connection"].Value;
    bool isDefault = dbStrings.Backup["DB1Connection"].Default;

    */
    /// <summary>
    /// DBConnectionStringSection  Web.Config 配置获取
    /// </summary>
    public class DBConnectionStringSection : System.Configuration.ConfigurationSection
    {
        /// <summary>
        /// 主库
        /// </summary>
        [ConfigurationProperty("Master", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
        [ConfigurationCollection(typeof(MasterSection))]
        public Master Master
        {
            get
            {
                return (Master)base["Master"];
            }
            set
            {
                base["Master"] = value;
            }
        }
        /// <summary>
        /// 备库
        /// </summary>
        [ConfigurationProperty("Backup", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(BackupSection))]
        public Backup Backup
        {
            get
            {
                return (Backup)base["Backup"];
            }
            set
            {
                base["Backup"] = value;
            }
        }
    }
    /// <summary>
    /// 主数据库节点
    /// </summary>
    public class MasterSection : System.Configuration.ConfigurationElement
    {
        /// <summary>
        /// 数据库名称
        /// </summary>
        [ConfigurationProperty("key", IsRequired = true, IsKey = true)]
        public string Key
        {
            get
            {
                return (string)base["key"];
            }
            set
            {
                base["key"] = value;
            }
        }
        /// <summary>
        /// 链接字符串
        /// </summary>
        [ConfigurationProperty("value", IsRequired = true)]
        public string Value
        {
            get
            {
                return Convert.ToString(base["value"]);
            }
            set
            {
                base["value"] = value;
            }
        }
        /// <summary>
        /// 数据库类型
        /// </summary>
        [ConfigurationProperty("type", IsRequired = true)]
        public string Type
        {
            get
            {
                return Convert.ToString(base["type"]);
            }
            set
            {
                base["type"] = value;
            }
        }
        /// <summary>
        /// 数据库默认链接
        /// </summary>
        [ConfigurationProperty("default", IsRequired = true)]
        public bool Default
        {
            get
            {
                return Convert.ToBoolean(base["default"]);
            }
            set
            {
                base["default"] = value;
            }
        }
    }

    /// <summary>
    /// 主数据库节点
    /// </summary>
    [ConfigurationCollection(typeof(MasterSection))]
    public class Master : System.Configuration.ConfigurationElementCollection
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((MasterSection)element).Key;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new MasterSection();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public MasterSection this[int i]
        {
            get
            {
                return (MasterSection)base.BaseGet(i);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public new MasterSection this[string key]
        {
            get
            {
                return (MasterSection)base.BaseGet(key);
            }
        }
    }

    /// <summary>
    /// 备库链接
    /// </summary>
    public class BackupSection : MasterSection
    { }
    /// <summary>
    /// 备库节点
    /// </summary>
    [ConfigurationCollection(typeof(BackupSection))]
    public class Backup : System.Configuration.ConfigurationElementCollection
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((BackupSection)element).Key;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new BackupSection();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public BackupSection this[int i]
        {
            get
            {
                return (BackupSection)base.BaseGet(i);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public new BackupSection this[string key]
        {
            get
            {
                return (BackupSection)base.BaseGet(key);
            }
        }
    }
}
/*
//测试代码

Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 10000; i++)
{
    string str = Utility.DBUtility.DBStringManage.Instance.GetDbString("BISConnection", Utility.DBUtility.DatabaseType.Backup);
}
sw.Stop();
long t = sw.ElapsedMilliseconds;

*/